$(function () {
    $("#news-div label.panel-header").collapsiblePanel({
        minimize:function () {
            $(this).hide();
        },
        restore:function () {
            $(this).show();
        }
    });

    $("#registerButton").click(function () {
        showConfirmDialog($("#regDialog"), function () {
            if (!validateForm()) {
                $("#signError").show();

                return;
            }

            $("#signError").hide();

            UserServiceRemote.register($("#regLogin").val(), $("#regPassword").val(), $("#name").val(), $("#email").val(),
                    {callback:function (result) {
                        if (!result) {
                            alert("Registration failed!");
                        } else {
                            alert("You have successfully registered!");

                            $("#regDialog").dialog("close");
                        }
                    }
                    });
        }, function () {
            $(this).dialog("close")
        }, false);
    });
});

var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
var ck_username = /^[A-Za-z0-9_]{1,20}$/;
var ck_password = /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;

function validateForm() {
    return ck_name.test($("#name").val()) && ck_email.test($("#email").val()) & ck_username.test($("#regLogin").val()) && ck_password.test($("#regPassword").val())
}